//
//  CYFinanceDetailInfoCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/8.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceDetailInfoCell.h"



@implementation CYFinanceDetailInfoCellObject

@end


@interface CYFinanceDetailInfoCell ()

@property(nonatomic, strong)UIImageView *topBackgroundImageView;
@property(nonatomic, strong)UILabel *rateLabel;
@property(nonatomic, strong)UILabel *rateDescLabel;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UIView *l1;
@property(nonatomic, strong)UIView *l2;
@property(nonatomic, strong)UILabel *totalDescLabel;
@property(nonatomic, strong)UILabel *totalLabel;
@property(nonatomic, strong)UILabel *dueDescLabel;
@property(nonatomic, strong)UILabel *dueLabel;
@property(nonatomic, strong)UILabel *limitDescLabel;
@property(nonatomic, strong)UILabel *limitLabel;
@property(nonatomic, strong)UILabel *balanceLabel;
@property(nonatomic, strong)UILabel *percentLabel;
@property(nonatomic, strong)UIView *progressBackgroundView;
@property(nonatomic, strong)UIImageView *progressImageView;

@property(nonatomic, strong)CYFinanceDetailInfoCellObject *object;

@end


@implementation CYFinanceDetailInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.topBackgroundImageView = [[UIImageView alloc] initWithImage:CYImage(@"finance_detail_top_bg")];
        [self.contentView addSubview:self.topBackgroundImageView];
        
        self.rateLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.rateLabel];

        self.rateDescLabel = [[UILabel alloc] init];
        self.rateDescLabel.text = @"预期年化收益率";
        self.rateDescLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.rateDescLabel.textColor = HEXCOLOR(@"999999");
        [self.rateDescLabel sizeToFit];
        [self.contentView addSubview:self.rateDescLabel];
        
        self.l1 = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.l1];
        
        self.l2 = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.l2];
        
        self.totalDescLabel = [[UILabel alloc] init];
        self.totalDescLabel.text = @"项目总额";
        self.totalDescLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.totalDescLabel.textColor = HEXCOLOR(@"999999");
        [self.totalDescLabel sizeToFit];
        [self.contentView addSubview:self.totalDescLabel];
        
        self.totalLabel = [[UILabel alloc] init];
        self.totalLabel.textColor = HEXCOLOR(@"444444");
        self.totalLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.totalLabel];
        
        self.dueDescLabel = [[UILabel alloc] init];
        self.dueDescLabel.text = @"投资期限";
        self.dueDescLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.dueDescLabel.textColor = HEXCOLOR(@"999999");
        [self.dueDescLabel sizeToFit];
        [self.contentView addSubview:self.dueDescLabel];
        
        self.dueLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.dueLabel];

        self.limitDescLabel = [[UILabel alloc] init];
        self.limitDescLabel.text = @"投资限额";
        self.limitDescLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.limitDescLabel.textColor = HEXCOLOR(@"999999");
        [self.limitDescLabel sizeToFit];
        [self.contentView addSubview:self.limitDescLabel];
        
        self.limitLabel = [[UILabel alloc] init];
        self.limitLabel.textColor = HEXCOLOR(@"444444");
        self.limitLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.limitLabel];
        
        self.line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.line];
        
        self.balanceLabel = [[UILabel alloc] init];
        self.balanceLabel.textColor = HEXCOLOR(@"444444");
        self.balanceLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.balanceLabel];
        
        self.percentLabel = [[UILabel alloc] init];
        self.percentLabel.textColor = HEXCOLOR(@"999999");
        self.percentLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.percentLabel];
        
        self.progressBackgroundView = [[UIView alloc] init];
        self.progressBackgroundView.backgroundColor = HEXCOLOR(@"ECECEC");
        [self.contentView addSubview:self.progressBackgroundView];
        
        self.progressImageView = [[UIImageView alloc] init];
        self.progressImageView.image = [UIImage gradientImageFromColors:@[HEXCOLOR(@"FE754D"), HEXCOLOR(@"FEDFC4")] ByGradientType:UIImageGradientTypeLeftToRight size:CGSizeMake(SCREEN_WIDTH, 3)];
        [self.contentView addSubview:self.progressImageView];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 240;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;

    if(self.object.model)
    {
        self.rateLabel.attributedText = [self rateAttributedStringWithRate:self.object.model.rate additionRate:self.object.model.additionRate];
        
        self.dueLabel.attributedText = [self dueAttributedStringWithDue:@(SAFE_NUMBER(self.object.model.apiModel.duration).longValue).stringValue];
        
        self.totalLabel.text = self.object.model.total;
        
        self.balanceLabel.text = [NSString stringWithFormat:@"%@起投　剩余可投%@", self.object.model.start, self.object.model.balance];
        
        self.percentLabel.text = [NSString stringWithFormat:@"已售%@%%", self.object.model.perCent];
        
        self.limitLabel.text = self.object.model.limit;
    }
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.topBackgroundImageView sizeToFit];
    self.topBackgroundImageView.width = self.contentView.width;
    self.topBackgroundImageView.left = 0;
    self.topBackgroundImageView.bottom = self.contentView.height - 60;
    
    [self.rateLabel sizeToFit];
    self.rateLabel.centerX = self.contentView.width / 2;
    self.rateLabel.centerY = 67;

    self.rateDescLabel.centerX = self.contentView.width / 2;
    self.rateDescLabel.centerY = 36;
    
    self.l1.height = 35;
    self.l1.width = 1 / SCREEN_SCALE;
    self.l1.top = 110;
    self.l1.centerX = (self.contentView.width - 38) / 3 + 19;
    
    self.l2.height = 35;
    self.l2.width = 1 / SCREEN_SCALE;
    self.l2.top = 110;
    self.l2.centerX = (self.contentView.width - 38) * 2 / 3 + 19;
    
    self.totalDescLabel.centerX = (self.contentView.width - 38) / 6 + 19;
    self.totalDescLabel.top = self.l1.top - 1;
    
    [self.totalLabel sizeToFit];
    self.totalLabel.centerX = self.totalDescLabel.centerX;
    self.totalLabel.bottom = self.l1.bottom + 1;
    
    self.dueDescLabel.centerX = (self.contentView.width - 38) * 3 / 6 + 19;
    self.dueDescLabel.top = self.l1.top - 1;
    
    [self.dueLabel sizeToFit];
    self.dueLabel.centerX = self.dueDescLabel.centerX;
    self.dueLabel.bottom = self.l1.bottom + 1;
    
    self.limitDescLabel.centerX = (self.contentView.width - 38) * 5 / 6 + 19;
    self.limitDescLabel.top = self.l1.top - 1;
    
    [self.limitLabel sizeToFit];
    self.limitLabel.centerX = self.limitDescLabel.centerX;
    self.limitLabel.bottom = self.l1.bottom + 1;
    
    self.line.width = self.contentView.width;
    self.line.height = 1 / SCREEN_SCALE;
    self.line.top = self.topBackgroundImageView.bottom;
    self.line.left = 0;
    
    [self.balanceLabel sizeToFit];
    self.balanceLabel.left = 20;
    self.balanceLabel.centerY = self.contentView.height - 37;
    
    [self.percentLabel sizeToFit];
    self.percentLabel.right = self.contentView.width - 20;
    self.percentLabel.centerY = self.balanceLabel.centerY;
    
    self.progressBackgroundView.width = self.contentView.width - 40;
    self.progressBackgroundView.height = 3;
    self.progressBackgroundView.centerX = self.contentView.width / 2;
    self.progressBackgroundView.bottom = self.contentView.height - 17;
    
    self.progressImageView.width = self.progressBackgroundView.width * self.object.model.perCent.doubleValue / 100;
    self.progressImageView.height = 3;
    self.progressImageView.left = self.progressBackgroundView.left;
    self.progressImageView.top = self.progressBackgroundView.top;
}

#pragma mark - Private Method
- (NSAttributedString *)dueAttributedStringWithDue:(NSString *)due
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    
    UIFont *font = [UIFont systemFontOfSize:16];
    UIColor *descColor = HEXCOLOR(@"444444");
    UIColor *dueColor = APP_DEFAULT_COLOR_RED;
    
    NSDictionary *descAttributes = @{NSFontAttributeName : font,
                                     NSForegroundColorAttributeName : descColor};
    NSDictionary *dueAttributes = @{NSFontAttributeName : font,
                                    NSForegroundColorAttributeName : dueColor};
    
    [string appendAttributedString:[[NSAttributedString alloc] initWithString:due attributes:dueAttributes]];
    [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"天" attributes:descAttributes]];
    
    return string;
}

- (NSAttributedString *)rateAttributedStringWithRate:(NSString *)rate additionRate:(NSString *)additionRate
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    
    UIFont *rateFont = [UIFont systemFontOfSize:34 weight:UIFontWeightRegular];
    UIFont *plusFont = [UIFont systemFontOfSize:26 weight:UIFontWeightRegular];
    UIColor *color = APP_DEFAULT_COLOR_RED;
    
    NSDictionary *rateAttributes = @{NSFontAttributeName : rateFont,
                                     NSForegroundColorAttributeName : color
                                     };
    NSDictionary *plusAttributes = @{NSFontAttributeName : plusFont,
                                     NSForegroundColorAttributeName : color
                                     };
    
    [string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%", rate] attributes:rateAttributes]];
    if(additionRate)
    {
        [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"+" attributes:plusAttributes]];
        [string appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%", additionRate] attributes:rateAttributes]];
    }
    return string;

}

@end
